<?php
session_start();
include 'nav.inc.php';
include 'dbConn.php';
html_head('Liste bearbeiten');

echo("<link rel='stylesheet' type='text/css' href='styleVerteiler.css' media='all' />\n");
echo "<script type='text/javascript' src='verteiler.js'></script>";

menu();

$lid = -1;
$doubleEntry = 0;
echo("<div class='content'>\n");
if (!isset($_REQUEST['submitBTN']) && isset($_REQUEST['lid'])) {
	$lid = $_REQUEST['lid'];
	
	$result = $dbVerteiler->getListByLID($lid);
	$user = mysql_fetch_array($result);
	$title = $user['Titel'];
	$content = $user['Kurzbeschreibung'];
	$result = $dbVerteiler->getAdressByLID($lid);
	$user = mysql_fetch_array($result);
	$email = $user['Adresse'];
} 

if (isset($_REQUEST['submitBTN']) || $_REQUEST['force'] > 0) 
{
	$lid = $_REQUEST['lid'];
	$title = $_REQUEST['title'];
	$content = $_REQUEST['content'];
	$email = $_REQUEST['email'];

	$resultCheck = $dbVerteiler->getListByTitle($title);
	$falseAlarm = false;
	if ($_REQUEST['force'] < 0 && mysql_num_rows($resultCheck) > 0) 
	{
		$onlymyself = true;
		while ($row = mysql_fetch_array($resultCheck)) 
		{
			$onlymyself = $onlymyself && $row["LID"] == $lid;
		}
		if (!$onlymyself)
		{
			$result = $dbVerteiler->getListByLID($lid);
			$row = mysql_fetch_array($result);
			echo "<script language=javascript>
					var nTitle=prompt('Achtung: \\n\\nEine Liste mit dem Titel \"".$title."\" exisitert bereits in der Datenbank. \\nBitte geben Sie einen eindeutigen Titel an, oder bestätigen sie den Doppeleintrag durch einen Klick auf \'OK\'.', '".$title."');
					var title = '".$row["Titel"]."';
					if (nTitle != null)	
					{
						title = nTitle;
					} 
					setModList(title);
				</script>";
			$doubleEntry = 1;
		} 
		else
			$falseAlarm = true;
	} 
	
	if (!($_REQUEST['force'] < 0) || !(mysql_num_rows($resultCheck) > 0)  || $falseAlarm)
	{
		$dbVerteiler->UpdateList($lid, $title, $content);
		$dbVerteiler->UpdateListAddress($lid, $email);
	}
}
echo "<form id='modList' name='modList' action='".$_SERVER['SCRIPT_NAME']."' method='post' >\n";
$result = $dbVerteiler->getLists();
echo "\t<fieldset>
  <legend>Wähle Liste</legend>
";
	echo "<div id='listselect'><select id='lists' name='lists' size=10 onchange='showListInfo();'>";
while ($row = mysql_fetch_array($result))
{
	echo "<option value='".$row['LID']."'>".$row['Titel']."</option>";
}
echo "</select></div>
  </fieldset>";
echo "\t<fieldset>
 <legend id='listInfoLegend' style='display:none;'>Neue Verteilerliste</legend>
  <ol id='listInfo' style='display:none;' class='clearfix'>
    <li>
      <label for='title'>Titel:</label>
      <input type='text' name='title' id='title' value='".$title."' onfocus='if (submit.value == \"Eingetragen\"){ colorReset();}'/>
    </li>
    <li>
      <label for='email'>E-Mail:</label>
      <input type='text' name='email' id='email' value='".$email."' onfocus='if (submit.value == \"Eingetragen\"){ colorReset();}'/>
      <input type='hidden' name='lid' id='lid' value='".$lid."' />
      <input type='hidden' name='index' id='index' value='-1' />
    </li>
    <li>
      <label for='content'>Kommentar (z.B. gedacht für):</label>
<textarea cols='32' rows='7' name='content' id='content' onfocus='if (submit.value == \"Eingetragen\"){ colorReset();}'>".$content."</textarea>
    </li>
    <li class='last'>
      		<input type='submit' name='submitBTN' id='submitBTN' value='Ändern' />
    </li>
  </ol>
  </fieldset>";
	echo "\t</form>\n";

if ($doubleEntry)
{
	echo "<script language=javascript>
				changeModList(".$lid.");
		</script>";
}
	
if ($lid > -1)
		echo "<script type='text/javascript'>showNowForRealList('".$_REQUEST["index"]."');</script>";
echo "</div>";

?>
